<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
                  <el-form-item label="开始时间" prop="startTime">
                    <el-date-picker clearable
                                    v-model="queryParams.startTime"
                                    type="date"
                                    value-format="YYYY-MM-DD"
                                    placeholder="请选择执行开始时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker clearable
                                    v-model="queryParams.endTime"
                                    type="date"
                                    value-format="YYYY-MM-DD"
                                    placeholder="请选择执行结束时间">
                    </el-date-picker>
                  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:command:remove']"
        >删除</el-button>
      </el-col>
      
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commandList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="模板名称" align="center" prop="name" />
              <el-table-column label="命令片段" align="center" prop="command" />
              <el-table-column label="参数定义" align="center" prop="parameterSchema" />
             <el-table-column label="状态" prop="status" align="center" width="120">
               <template #default="scope">
               <dict-tag :options="command_status" :value="scope.row.status" />
            </template>
              </el-table-column>
              <el-table-column label="执行开始时间" align="center" prop="startTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.startTime,'{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="执行结束时间" align="center" prop="endTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                </template>
              </el-table-column>
                <el-table-column label="用户名称" align="center" prop="userName" />
              <el-table-column label="超时时间" align="center" prop="timeout" />
              <el-table-column label="创建时间" align="center" prop="endTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                </template>
              </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="detail(scope.row.id)" v-hasPermi="['exec:command:query']">详情</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['exec:command:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

       <!-- 弹框详情列表 -->
  <el-dialog
  v-model="open"
  :title="title"
  width="80%"
  @close="cancel"
>
  <el-form :inline="true" :model="detailQueryParams" class="mb8">
    <el-form-item label="地址" prop="hostAddress">
      <el-input v-model="detailQueryParams.hostAddress" placeholder="请输入地址" clearable @keyup.enter="getDetailList" />
    </el-form-item>
    <el-form-item label="状态" prop="status" >
      <el-select v-model="detailQueryParams.status" placeholder="请选择状态" clearable style="min-width: 120px;">
        <el-option
          v-for="item in command_status"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="Search" @click="getDetailList">搜索</el-button>
      <el-button icon="Refresh" @click="resetTaskDetailQuery">重置</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="taskDetailList" v-loading="detailLoading" style="width: 100%">
    <el-table-column label="主机名" prop="hostName" />
    <el-table-column label="地址" prop="hostAddress" />
  
     <el-table-column label="开始时间" align="center" prop="startTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                </template>
              </el-table-column>
                <el-table-column label="结束时间" align="center" prop="endTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                </template>
              </el-table-column>
                <el-table-column label="状态" prop="status">
      <template #default="scope">
        <dict-tag :options="command_status" :value="scope.row.status" />
      </template>
    </el-table-column>
    <el-table-column label="备注" prop="errorMessage" />
  </el-table>
  <pagination
    v-show="detailTotal>0"
    :total="detailTotal"
    v-model:page="detailQueryParams.pageNum"
    v-model:limit="detailQueryParams.pageSize"
    @pagination="getDetailList"
  />
</el-dialog>

  </div>
</template>

<script setup name="Command">
import { listCommandLog, delCommand,listCommand } from "@/api/exec/command"
import { ref, reactive, toRefs, getCurrentInstance } from "vue"

const { proxy } = getCurrentInstance()
const { command_status } = proxy.useDict("command_status")
const commandList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('') // 新增
const taskDetailList = ref([]) // 新增

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    hostGroupId: null,
    execTemplateId: null,
    name: null,
    command: null,
    parameterSchema: null,
    status: null,
    startTime: null,
    endTime: null,
    timeout: null,
  },
  rules: {}
})

const { queryParams, form, rules } = toRefs(data)

function getList() {
  loading.value = true
  listCommand(queryParams.value).then(response => {
    commandList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

function cancel() {
  open.value = false
  resetTaskDetailQuery()
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

const detailTotal = ref(0)
const detailLoading = ref(false)
const detailQueryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  hostAddress: '',
  status: ''
})
let currentTaskId = null

function detail(id) {
  open.value = true
  title.value = "命令执行详情"
  currentTaskId = id
  resetTaskDetailQuery()
  getDetailList()
}

function getDetailList() {
  detailLoading.value = true
  listCommandLog(currentTaskId, {
    ...detailQueryParams
  }).then(res => {
    taskDetailList.value = res.rows
    detailTotal.value = res.total
    detailLoading.value = false
  }).catch(() => {
    detailLoading.value = false
  })
}

function resetTaskDetailQuery() {
  detailQueryParams.pageNum = 1
  detailQueryParams.pageSize = 10
  detailQueryParams.hostAddress = ''
  detailQueryParams.status = ''
}

function handleDelete(row) {
  const _ids = row?.id ? [row.id] : ids.value
  proxy.$modal.confirm('是否确认删除命令执行编号为"' + _ids + '"的数据项？').then(function() {
    return delCommand(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

getList()
</script>
